package pl.edu.pw.elka.jtrzasko.proz.warcaby.actions.server;

import java.util.Iterator;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import pl.edu.pw.elka.jtrzasko.proz.warcaby.figures.Figure;
import pl.edu.pw.elka.jtrzasko.proz.warcaby.messages.server.ServerMessage;
import pl.edu.pw.elka.jtrzasko.proz.warcaby.messages.server.ServerMessageBoard;
import pl.edu.pw.elka.jtrzasko.proz.warcaby.view.ClientView;

public class ServerActionBoard extends ServerAction {

	private static final int LINE_MAX = 8;

	public void run(final ClientView clientView, final ServerMessage message){
	
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				Figure figure;
				for (int i = 0; i < LINE_MAX; i++)
					for (int j = 0; j < LINE_MAX; j++)
						clientView.getBoardPanels()[i][j].removeAll();
				Iterator<Figure> iterator = ((ServerMessageBoard)message).getFigures().iterator();
				while (iterator.hasNext())
				{
					figure = iterator.next();
//					JAR
					JLabel imageLabel = new JLabel(new ImageIcon(ClassLoader.getSystemResource(figure.getClass().getSimpleName().toString() + "_" + figure.getColor().toString()+ ".gif")));
//					JLabel imageLabel = new JLabel(new ImageIcon("images/" + figure.getClass().getSimpleName().toString() + "_" + figure.getColor().toString()+ ".gif"));
					int a = figure.getPosition().getX();
					int b = figure.getPosition().getY();
					clientView.getBoardPanels()[a][b].removeAll();
					clientView.getBoardPanels()[a][b].add(imageLabel);
					clientView.getBoardPanels()[a][b].validate();
				}
				clientView.repaint();

			}
		});
	}

}
